package ldkxingzhe.ldkxingzhedemo.widgets.treeview;


import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xingzhe on 15/11/19.
 */
public class TreeNode {
    String mName;
    List<TreeNode> mChildren = new ArrayList<>();
    Object mObj;

    /**
     * 设置节点名称
     * @param name   节点名称, @NonNull
     */
    public void setName(@NonNull String name){
        this.mName = name;
    }
    public String getName(){
        return mName;
    }
    /**
     * 添加子节点
     * @param child   子节点, @NonNull
     */
    public void addChild(@NonNull TreeNode child){
        checkLegal();
        mChildren.add(child);
    }
    // 检测判断
    private void checkLegal() {
        if(mObj != null){throw new IllegalStateException("mObj is not null");}
        if (mChildren == null){
            mChildren = new ArrayList<>();
        }
    }
    /**
     * 获取该节点所有子节点的名称列表
     * */
    public List<String> getChildrenNameList(){
        List<String> result = new ArrayList<>();
        for(TreeNode treeNode : mChildren){
            result.add(treeNode.getName());
        }
        return result;
    }

    public int getChildrenCount(){
        return mChildren.size();
    }

    /**
     * 获取position处的子节点
     * @param position    position
     * @return treeNode
     */
    @NonNull
    public TreeNode getTreeNodeAt(int position){
        return mChildren.get(position);
    }
    /**
     * 获取节点中包含的对象
     * */
    public Object getObj(){
        return mObj;
    }

    public void setObj(@NonNull Object mObj) {
        this.mObj = mObj;
    }
}
